package sz.com.postoffice.jxyzwgcase.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import static com.github.drinkjava2.jdbpro.JDBPRO.notNull;
import static com.github.drinkjava2.jdbpro.JDBPRO.sql;

import io.jsonwebtoken.lang.Collections;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.drinkjava2.jsqlbox.DbContext;
import com.sun.istack.NotNull;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import sz.com.postoffice.jxyzwgcase.core.PageDomain;
import sz.com.postoffice.jxyzwgcase.core.TableDataInfo;
import sz.com.postoffice.jxyzwgcase.core.TableSupport;
import sz.com.postoffice.jxyzwgcase.pojo.ResourceCustomerRelation;
import sz.com.postoffice.jxyzwgcase.pojo.ResourceData;
import sz.com.postoffice.jxyzwgcase.pojo.ResourceMarket;
import sz.com.postoffice.jxyzwgcase.service.EnterpriseCustomerService;
import sz.com.postoffice.jxyzwgcase.service.ResourceMarketService;
import sz.com.postoffice.jxyzwgcase.utils.StringUtils;

/**
 * @ClassName ResourceController
 * @Deacription
 * @Author jmx
 * @Date 2020/11/20 19:52
 * @Version 1.0
 **/
@Api(value = "资源(市场)信息", tags = "{资源(市场)信息API}")
@RestController
@Slf4j
@RequestMapping(value = "/resource")
public class ResourceMarketController {
    @Resource
    private ResourceMarketService marketService;
    @Resource
    private EnterpriseCustomerService enterpriseCustomerService;

    @ApiOperation(value = "新增市场", notes = "新增市场")
    @RequestMapping(value = "/markets", method = RequestMethod.POST)
    public TableDataInfo insert(@RequestBody ResourceMarket market) {
        System.out.println("开始新增...");
        TableDataInfo dataInfo = new TableDataInfo();
        if (StringUtils.isEmpty(market.getCode())) {
            dataInfo.setCode(400);
            dataInfo.setMsg("code不能为空");
            return dataInfo;
        }
        ResourceMarket param = new ResourceMarket();
        param.setCode(market.getCode());
        param.setResourceType(market.getResourceType());
        List<ResourceMarket> search = marketService.findListByType(param);
        if (Collections.isEmpty(search)) {
            if (marketService.insertByType(market)) {
                dataInfo.setCode(200);
                dataInfo.setMsg("新增成功");
            } else {
                dataInfo.setCode(500);
                dataInfo.setMsg("新增失败");
            }
            return dataInfo;
        } else {
            dataInfo.setCode(400);
            dataInfo.setMsg("同一段道下资源名称不能重复");
        }
        return dataInfo;
    }

    @ApiOperation(value = "修改市场", notes = "修改市场")
    @RequestMapping(value = "/markets", method = RequestMethod.PUT)
    public boolean update(@RequestBody ResourceMarket market) {
        System.out.println("开始修改...");
        return marketService.updateByType(market);
    }

    @ApiOperation(value = "根据id和类型查询市场详情", notes = "根据id和类型查询市场详情")
    @RequestMapping(value = "/market", method = RequestMethod.GET)
    public ResourceMarket findById(ResourceMarket market) {
        System.out.println("开始查询...");
        return marketService.findByTypeAndId(market);
    }

    @ApiOperation(value = "根据code删除市场详情", notes = "根据code删除市场详情")
    @RequestMapping(value = "/market/{type}/{code}", method = RequestMethod.DELETE)
    public TableDataInfo deleteById(@PathVariable("type") @NotNull @ApiParam(name="资源类型",value="path传入",required=true) Integer type,
            @PathVariable("code") @NotNull @ApiParam(name="资源code",value="path传入",required=true) String code) {
        System.out.println("开始删除...");
        TableDataInfo dataInfo = new TableDataInfo();
        //根据code查询关联关系
        ResourceCustomerRelation relation = enterpriseCustomerService.getRelationByResource(code);
        if (relation != null) {
            dataInfo.setMsg("该资源已关联商企，不能删除");
            dataInfo.setCode(400);
            return dataInfo;
        } else {
            ResourceMarket market = new ResourceMarket();
            market.setResourceType(type);
            market.setCode(code);
            if (marketService.deleteByTypeAndCode(market)) {
                dataInfo.setMsg("删除成功");
                dataInfo.setCode(200);
            } else {
                dataInfo.setMsg("删除失败");
                dataInfo.setCode(500);
            }
        }
        return dataInfo;
    }

    @ApiOperation(value = "查询市场信息列表", notes = "查询市场信息列表")
    @RequestMapping(value = "/markets", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", dataType = "int", paramType = "path", required = true),
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataType = "int", paramType = "path", required = true)
    })
    public TableDataInfo findListByEntity(ResourceMarket market) {
        System.out.println("开始查询...");
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        return marketService.findPageListByType(market, pageSize, pageNum);
    }

	@ApiOperation(value = "获取市场列表信息", notes = "获取市场列表信息")
	@RequestMapping(value = "/marketList", method = RequestMethod.GET)
	public List<ResourceMarket> getMarketList(ResourceMarket market) {
		return marketService.findListByType(market);
	}
}
